package com.xingyi.bacs.util;

import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@Component
public class JDBCUtil {

	private static String qs_url;

	private static String qs_username;

	private static String qs_password;

	private static String qs_driverClassName;

	private static String fr_url;

	private static String fr_username;

	private static String fr_password;

	private static String fr_driverClassName;

	/**
	 * 加载驱动及创建Connection对象
	 * @return
	 */
	public static Connection getQSConn(){
		Connection connection = null;
		try {
			Class.forName(qs_driverClassName);
			String p = qs_password;
			connection = DriverManager.getConnection(qs_url, qs_username, p);
		} catch (Exception e) {
			connection = null;
			e.printStackTrace();
		}
		return connection;
	}

	/**
	 * 加载驱动及创建Connection对象
	 * @return
	 */
	public static Connection getFRConn(){
		Connection connection = null;
		try {
			Class.forName(fr_driverClassName);
			String p = fr_password;
			connection = DriverManager.getConnection(fr_url, fr_username, p);
		} catch (Exception e) {
			connection = null;
			e.printStackTrace();
		}
		return connection;
	}

	/**
	 * 关闭数据库资源
	 * @param connection
	 * @param statement
	 * @param resultSet
	 */
	public static void getClose(Connection connection,Statement statement,ResultSet resultSet){
		try {
			if(connection != null){
				connection.close();
			}
			if(statement != null){
				statement.close();
			}
			if(resultSet != null){
				resultSet.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Value("${spring.datasource.dynamic.datasource.qs.url}")
	public void setQs_url(String qs_url) {
		JDBCUtil.qs_url = qs_url;
	}
	@Value("${spring.datasource.dynamic.datasource.qs.username}")
	public void setQs_username(String qs_username) {
		JDBCUtil.qs_username = qs_username;
	}
	@Value("${spring.datasource.dynamic.datasource.qs.password}")
	public void setQs_password(String qs_password) {
		try {
			JDBCUtil.qs_password = CryptoUtils.decrypt(qs_password.substring(4,qs_password.length()-1));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Value("${spring.datasource.dynamic.datasource.qs.driver-class-name}")
	public void setQs_driverClassName(String qs_driverClassName) {
		JDBCUtil.qs_driverClassName = qs_driverClassName;
	}
	@Value("${spring.datasource.dynamic.datasource.fr.url}")
	public void setFr_url(String fr_url) {
		JDBCUtil.fr_url = fr_url;
	}
	@Value("${spring.datasource.dynamic.datasource.fr.username}")
	public void setFr_username(String fr_username) {
		JDBCUtil.fr_username = fr_username;
	}
	@Value("${spring.datasource.dynamic.datasource.fr.password}")
	public void setFr_password(String fr_password) {
		try {
			JDBCUtil.fr_password = CryptoUtils.decrypt(fr_password.substring(4,fr_password.length()-1));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Value("${spring.datasource.dynamic.datasource.fr.driver-class-name}")
	public void setFr_driverClassName(String fr_driverClassName) {
		JDBCUtil.fr_driverClassName = fr_driverClassName;
	}
}
